package com.soft.jdbc.customer.service;

import com.soft.jdbc.customer.dao.CustomerDAO;
import com.soft.jdbc.customer.entity.Customer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/6/6
 */
public class CustomerService {
    private CustomerDAO customerDao = new CustomerDAO();
    private Scanner sc = new Scanner(System.in);

    // 插入客户
    public void insertCustomer() {
        System.out.println("请输入编号：");
        int id = Integer.parseInt(sc.nextLine());
        System.out.println("请输入姓名：");
        String name = sc.nextLine();
        System.out.println("请输入邮箱：");
        String email = sc.nextLine();
        System.out.println("请输入生日（要求按xxxx-xx-xx格式）：");
        String birthdayStr = sc.nextLine();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date birthday = sdf.parse(birthdayStr);

            Customer customer = new Customer(id, name, email, birthday);
            boolean success = customerDao.insertCustomer(customer);
            if (success) {
                System.out.println("插入成功！");
            } else {
                System.out.println("插入失败");
            }
        } catch (ParseException e) {
            System.out.println("日期格式不正确，请使用yyyy-MM-dd格式");
        }
    }

    // 修改客户信息
    public void updateCustomer() {
        System.out.println("请输入待修改的客户编号：");
        int id = Integer.parseInt(sc.nextLine());

        Customer customer = customerDao.getCustomerById(id);
        if (customer == null) {
            System.out.println("未找到编号为 " + id + " 的客户");
            return;
        }

        System.out.println("请输入新的客户姓名：");
        String newName = sc.nextLine();

        customer.setName(newName);
        boolean success = customerDao.updateCustomer(customer);
        if (success) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败");
        }
    }

    // 查询所有客户
    public void listAllCustomers() {
        List<Customer> customers = customerDao.getAllCustomers();
        System.out.println("----------------------------------------");
        System.out.println("编号\t\t姓名\t\t邮箱\t\t\t\t生日");
        for (Customer customer : customers) {
            System.out.println(customer);
        }
    }

    // 按ID查询客户
    public void queryCustomerById() {
        System.out.println("请输入编号：");
        int id = Integer.parseInt(sc.nextLine());

        Customer customer = customerDao.getCustomerById(id);
        System.out.println("----------------------------------------");
        System.out.println("编号\t\t姓名\t\t邮箱\t\t\t\t生日");
        if (customer != null) {
            System.out.println(customer);
        } else {
            System.out.println("未找到编号为 " + id + " 的客户");
        }
    }

    // 按姓名查询客户
    public void queryCustomersByName() {
        System.out.println("请输入姓名：");
        String name = sc.nextLine();

        List<Customer> customers = customerDao.getCustomersByName(name);
        System.out.println("----------------------------------------");
        System.out.println("编号\t\t姓名\t\t邮箱\t\t\t\t生日");
        if (!customers.isEmpty()) {
            for (Customer customer : customers) {
                System.out.println(customer);
            }
        } else {
            System.out.println("查无此人");
        }
    }

    // 显示菜单
    public void showMenu() {
        System.out.println("1. 添加客户");
        System.out.println("2. 修改客户");
        System.out.println("3. 查询所有客户");
        System.out.println("4. 按编号查询客户");
        System.out.println("5. 按姓名查询客户");
        System.out.println("0. 退出");
    }

    public void run() {
        while (true) {
            showMenu();
            System.out.println("请选择操作(0-5):");
            String choice = sc.nextLine();

            switch (choice) {
                case "1":
                    insertCustomer();
                    break;
                case "2":
                    updateCustomer();
                    break;
                case "3":
                    listAllCustomers();
                    break;
                case "4":
                    queryCustomerById();
                    break;
                case "5":
                    queryCustomersByName();
                    break;
                case "0":
                    System.out.println("系统退出，谢谢使用！");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }
    }
}
